package com.rockcent.service.impl;

import com.rockcent.common.common.PageResult;
import com.rockcent.mall.lib.domain.MallMember;
import com.rockcent.mall.lib.domain.MallMemberFundFlow;
import com.rockcent.mall.lib.domain.RockcentFund;
import com.rockcent.mall.lib.domain.enumclass.MallMemberFundTransactionType;
import com.rockcent.mall.lib.domain.enumclass.MallMemberStatus;
import com.rockcent.mall.lib.dto.MallMemberDto;
import com.rockcent.mall.lib.repository.MallMemberFundFlowRepository;
import com.rockcent.mall.lib.repository.MallMemberRepository;
import com.rockcent.mall.lib.repository.RockcentFundRepository;
import com.rockcent.service.MallMemberSrv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * Created by zhangjh on 2017/3/8.
 */
@Service
public class MallMemberSrvImpl implements MallMemberSrv {

    @Autowired
    private RockcentFundRepository rockcentFundRepository;

    @Autowired
    private MallMemberRepository mallMemberRepository;

    @Autowired
    private MallMemberFundFlowRepository mallMemberFundFlowRepository;


    @Override
    public void changeMallMemberFund(BigDecimal operationAmount, Long mallMemberId, MallMemberFundTransactionType mallMemberRechargeType, String seqNo) {
        MallMemberFundFlow mallMemberFundFlow = new MallMemberFundFlow();
        MallMember mallMember = mallMemberRepository.findOne(mallMemberId);
        if (MallMemberFundTransactionType.OUTLINE_RECHARGE == mallMemberRechargeType || MallMemberFundTransactionType.RECHARGE_PRESENTED == mallMemberRechargeType || MallMemberFundTransactionType.RECHARGE_COMMON == mallMemberRechargeType) {
            //乐宸账户加钱
            RockcentFund rockcentFund = rockcentFundRepository.findOne(1L);
            rockcentFund.setTotalAmount(rockcentFund.getTotalAmount().add(operationAmount));
            rockcentFund.setActualRechargeAmount(rockcentFund.getActualRechargeAmount().add(operationAmount));
            rockcentFundRepository.save(rockcentFund);

            //平台会员账户加钱
            mallMember.setBalance(mallMember.getBalance().add(operationAmount));

            //是否是首次充值
            if (MallMemberStatus.AUDITING == mallMember.getStatus() || MallMemberStatus.AUDIT_REFUSE == mallMember.getStatus()) {
                mallMember.setStatus(MallMemberStatus.MEMBER);
                mallMember.setEffectDate(new Date());
            }

            mallMemberFundFlow.setBalance(mallMember.getBalance());
        }

        if (MallMemberFundTransactionType.BASE_SERVICE_PAYMENTS == mallMemberRechargeType || MallMemberFundTransactionType.MALL_EXCHANGE == mallMemberRechargeType || MallMemberFundTransactionType.MERCHANT_SERVICE_PAYMENTS == mallMemberRechargeType) {
            //平台会员账户减钱
            mallMember.setBalance(mallMember.getBalance().subtract(operationAmount));

            mallMemberFundFlow.setBalance(mallMember.getBalance());
        }

        mallMemberRepository.save(mallMember);

        //平台会员资金流水

        mallMemberFundFlow.setMallId(mallMember.getMallId());
        mallMemberFundFlow.setRemark(mallMemberRechargeType.getValue());
        mallMemberFundFlow.setMallMemberId(mallMemberId);
        mallMemberFundFlow.setOperationAmount(operationAmount);
        mallMemberFundFlow.setTransactionType(mallMemberRechargeType);
        mallMemberFundFlow.setSeqNo(seqNo);
        mallMemberFundFlowRepository.save(mallMemberFundFlow);
    }

    @Override
    public MallMember findTop1ByMallId(Long mallId) {
        return mallMemberRepository.findTop1ByMallId(mallId);
    }

    @Override
    public MallMember findTop1ByMallIdByLastUpdatedDesc(Long mallId) {
        return mallMemberRepository.findTop1ByMallIdOrderByLastUpdatedDesc(mallId);
    }

    @Override
    public MallMember save(MallMember mallMember) {
        return mallMemberRepository.save(mallMember);

    }

    @Override
    public PageResult<MallMemberDto> findMallMemberPage(MallMemberDto dto, PageResult page) {
        return mallMemberRepository.findMallMemberPage(dto,page);
    }

    @Override
    public MallMember findOneById(Long mallMemberId) {
        return mallMemberRepository.findOne(mallMemberId);
    }
}
